package com.platform.modules.wallet.controller;

import cn.hutool.core.util.StrUtil;
import com.platform.common.constant.AppConstants;
import com.platform.common.shiro.ShiroUtils;
import com.platform.common.version.ApiVersion;
import com.platform.common.version.VersionEnum;
import com.platform.common.web.domain.AjaxResult;
import com.platform.modules.chat.domain.ChatFriend;
import com.platform.modules.chat.domain.ChatUser;
import com.platform.modules.chat.service.ChatFriendService;
import com.platform.modules.chat.service.ChatUserService;
import com.platform.modules.push.enums.PushMsgTypeEnum;
import com.platform.modules.push.service.ChatPushService;
import com.platform.modules.wallet.domain.WalletTransfer;
import com.platform.modules.wallet.enums.TransferTypeEnum;
import com.platform.modules.wallet.service.WalletTransferService;
import com.platform.modules.wallet.vo.TransferVo02;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 钱包-转账
 */
@RestController
@Slf4j
@RequestMapping("/wallet")
public class TransferController {


    @Resource
    private WalletTransferService walletTransferService;
    @Resource
    private ChatUserService chatUserService;
    @Resource
    private ChatPushService chatPushService;
    @Resource
    private ChatFriendService friendService;

    /**
     * 查询转账信息
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @GetMapping("/getTransfer/{queryId}")
    public AjaxResult getTransfer(@PathVariable String queryId) {

        WalletTransfer transfer = walletTransferService.getByQueryId(queryId);
        return AjaxResult.success(transfer);
    }


    /**
     * 转账接收
     */
    @ApiVersion(VersionEnum.V1_0_0)
    @PostMapping("/transferReceive")
    public AjaxResult transferReceive(@Validated @RequestBody TransferVo02 vo) {


        Long userId = ShiroUtils.getUserId();

        walletTransferService.receive(vo.getId(),userId);

        //TODO 下面先这么做着 以后再封装优化
        WalletTransfer transfer = walletTransferService.getByQueryId(vo.getId());

        ChatUser to = chatUserService.getById(transfer.getToId());
        String content = StrUtil.format(AppConstants.TRANSFER_RECEIVE_NOTICE, to.getNickName());

        if (transfer.getType().equals(TransferTypeEnum.SINGLE)){
            // 发送通知
            ChatFriend friend = friendService.getFriend(transfer.getFromId(), transfer.getToId());
            chatPushService.pushMsg(ChatUser.initParam(to)
                    .setContent(content)
                    .setToId(transfer.getFromId())
                    .setTop(friend.getTop()), PushMsgTypeEnum.ALERT);

            friend = friendService.getFriend(transfer.getToId(), transfer.getFromId());
            ChatUser from = chatUserService.getById(transfer.getFromId());
            chatPushService.pushMsg(ChatUser.initParam(from)
                    .setContent(content)
                    .setToId(transfer.getToId())
                    .setTop(friend.getTop()), PushMsgTypeEnum.ALERT);
        }

        return AjaxResult.success("收款成功");
    }


}
